﻿package  
{
	import com.adobe.kuler.events.GetResultEvent;
	import com.adobe.kuler.params.SearchParams;
	import com.adobe.kuler.swatches.swatch.color.Color;
	import flash.display.MovieClip;
	import com.adobe.kuler.KLibService;
	import flash.display.Sprite;	
	
	/**
	 * This is a example of using klib to get swatches from Adobe's kuler API.
	 */
	public class Testing extends MovieClip
	{		
		public function Testing() 
		{
			var klib:KLibService = new KLibService("[YOUR API KEY HERE]"); //Get an API get at http://kuler.adobe.com/api/
			klib.addEventListener(GetResultEvent.GET_RESULTS, onResults);
			
			//Here is a list of example ways to use the kuler API. Just uncomment the one you want to use.
			
			
			//getRecent search example
			klib.getRecent(); 
			
			
			//getHighestRated search exmaple
			//klib.getHighestRated();
			
			
			//getPopular search example
			//klib.getPopular();
			
			
			//getRandom search example
			//klib.getRandom();
			
			
			//Example of email search
			//var temp:SearchParams = new SearchParams(SearchParams.EMAIL, "joshchernoff@msn.com");			
			//klib.searchSwatches(temp);
			
			
			//Example of tag search
			//var temp:SearchParams = new SearchParams(SearchParams.TAG, "dog");			
			//klib.searchSwatches(temp);
			
			//Example of hex search
			//var temp:SearchParams = new SearchParams(SearchParams.HEX, "0x00ff00");			
			//klib.searchSwatches(temp);
			
			
			//Example of THEME_ID search
			//var temp:SearchParams = new SearchParams(SearchParams.THEME_ID, "376159");			
			//klib.searchSwatches(temp);
			
			//Example of TITLE search
			//var temp:SearchParams = new SearchParams(SearchParams.TITLE, "Lady and the Tramp");			
			//klib.searchSwatches(temp);
			
			//Example of USER_ID search
			//var temp:SearchParams = new SearchParams(SearchParams.USER_ID, "211483");			
			//klib.searchSwatches(temp);		
			
			
		}		
		private function onResults(e:GetResultEvent):void 
		{
			trace(e);
			trace("recordCount", e.results.recordCount)
			trace("startIndex", e.results.startIndex);
			trace("itemsPerPage", e.results.itemsPerPage);
			
			var i:int = 0;
			for each(var swatch:Object in e.results.swatches)
			{				
				//Make a blank holder per swatch
				var swatchHolder:Sprite = new Sprite();
				addChild(swatchHolder);
				//place it's x and y
				swatchHolder.y = i++ * 125;
				
				//Fill the colors per swatch
				var j:int = 0;
				for each(var color:Color in swatch.colors)
				{
					swatchHolder.graphics.lineStyle(0,0,0);
					swatchHolder.graphics.beginFill(int("0x"+color.swatchHexColor)); //Get color of each color in this swatch per loop.
					swatchHolder.graphics.drawRect(j++ * 100,0, 100,100);
					swatchHolder.graphics.endFill();
				}		
			}
		}		
	}	
}